﻿using NFine.Code;
using NFine.Data;
using NFine.Domain._03_Entity.Biz;
using NFine.Domain._04_IRepository.Biz;
using System;
using System.Collections.Generic;

namespace NFine.Repository.Biz
{
    public class HuangouIntegralRepository : RepositoryBase<HuangouIntegralEntity>, IHuangouIntegralRepository
    {

        //private Log logger = LogFactory.GetLogger("HuangouIntegralRepository");
        IZhongZhuanPinJianRepository zzRep = new ZhongZhuanPinJianRepository();

        public bool Send()
        {
            return Send(DateTime.Now);
        }
        /// <summary>
        /// 获取中转中心现有积分
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        //public decimal GetZhZhJF(string userid)
        //{
        //    return GetSingleToDecimal("SELECT   `F_Integral`FROM `biz_zhzh_pinjian` t  WHERE t.`F_UserID`='"+userid+ "' ORDER BY `F_CreateTime` DESC LIMIT 0, 1");
        //}

        public bool Send(DateTime dt)
        {
            var list = FindList(t => t.F_Count_late > 0);
            Dictionary<string, decimal> dicIntegral = new Dictionary<string, decimal>();
            Dictionary<string, string> dicInfo = new Dictionary<string, string>();
           // logger.Debug("要发放奖金的记录条数："+list.Count);
            if (list == null)
                return false;
            foreach (var hgEntity in list)
            {
                if (!dicIntegral.ContainsKey(hgEntity.F_UserID))
                {
                    dicIntegral.Add(hgEntity.F_UserID, 0);
                    dicInfo.Add(hgEntity.F_UserID, "返利的订单编号：");
                    //dicIntegral[hgEntity.F_UserID] +=hgEntity.F_Count_late==1?hgEntity.F_Integral_late:;
                }
                var lo = decimal.Round(hgEntity.F_Integral_All / hgEntity.F_Count_All, 2);
                hgEntity.F_Count_late -= 1;
                if (hgEntity.F_Count_late == 0)
                {
                    dicIntegral[hgEntity.F_UserID] += hgEntity.F_Integral_late;
                    hgEntity.F_Integral_late = 0;
                }
                else
                {
                    dicIntegral[hgEntity.F_UserID] += lo;
                    hgEntity.F_Integral_late -= lo;
                }
                dicInfo[hgEntity.F_UserID] += hgEntity.F_OrderID + ",";
            }

           // logger.Debug("要发放奖金的人数：" + dicIntegral.Count);
            List<ZhongZhuanPinJianEntity> listZz = new List<ZhongZhuanPinJianEntity>();
            foreach (var item in dicIntegral)
            {
                ZhongZhuanPinJianEntity zzEntity = new ZhongZhuanPinJianEntity();
                zzEntity.F_Id = Code.Common.CreateNo();
                zzEntity.F_CreateTime = dt;
                zzEntity.F_Integral = zzRep.GetZhZhJF(item.Key) + item.Value;
                zzEntity.F_JLIntegral = item.Value;
                zzEntity.F_UserID = item.Key;
                zzEntity.F_Info = dicInfo[item.Key];
                zzEntity.F_Type = 1;
                listZz.Add(zzEntity);
            }
          //  logger.Debug("要发放奖金的人数：" + listZz.Count+"(构造后的实体数量)");
            using (var db = new RepositoryBase().BeginTrans())
            {
                foreach (var hgEntity in list)
                {
                    db.Update(hgEntity);
                }
                db.Insert(listZz);
                return db.Commit() > 0;
            }
        }
    }
}